//
//  BIZLiveRoomTableViewCellNew.m
//  biz
//
//  Created by zhangfuwei on 2017/3/31.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZLiveRoomTableViewCellNew.h"
#import "Utility.h"
#import "UIImage+Extension.h"

#define kContentFontSize        16.0f   //内容字体大小
#define kPadding                5.0f    //控件间隙
#define kEdgeInsetsWidth       20.0f   //内容内边距宽度
#define kWidth [UIScreen mainScreen].bounds.size.width //获取设备的物理宽度
#define kHeight [UIScreen mainScreen].bounds.size.height //获取设备的物理高度
static CGFloat kViewMargin = 15;
static CGFloat kTitleLabH = 20;
static NSString *kCellIdentifier = @"BIZLiveRoomTableViewCell";


@interface BIZLiveRoomTableViewCellNew ()

/** 名称 */
@property (nonatomic, retain) UILabel      *titleLabel;
/** 背景图 */
@property (nonatomic, retain) UIView      *bgView;
/** 文字内容视图 */
@property (nonatomic, retain) UILabel       *contentLabel;
/** 内容是否超过一行 */
@property (nonatomic, assign) BOOL moreThan1;

@end

@implementation BIZLiveRoomTableViewCellNew

#pragma mark - life cirle method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return self;
}

- (void)initSubViews
{
    //    //初始化正文视图
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.4;
    _bgView.layer.cornerRadius = 20;
    _bgView.clipsToBounds = YES;
    [self.contentView addSubview:_bgView];
    
    //初始化名称视图
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FONT(CHINESE_FONT_NAME, 14);
    _titleLabel.numberOfLines = 1;
    _titleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView insertSubview:_titleLabel aboveSubview:_bgView];
    
    //初始化正文视图
    _contentLabel = [[UILabel alloc] init];
//    _contentLabel.font = FONT(CHINESE_FONT_NAME, 14);
    _contentLabel.textColor = [UIColor whiteColor];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.numberOfLines = 0;
    [self.contentView insertSubview:_contentLabel aboveSubview:_bgView];

}

//- (void)layoutSubviews {
//    [super layoutSubviews];
//    
//    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.contentView);
//        make.bottom.equalTo(self.contentView).offset(-10);
//        make.left.equalTo(self.contentView).offset(kViewMargin);
//        make.right.equalTo(self.contentView).offset(-1*kViewMargin);
//        
//    }];
//    CGFloat titleLabW = [_titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, kTitleLabH)].width;
//    DeLog(@"标题文案===================%@--%lf",_titleLabel.text,titleLabW);
//    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_bgView).offset(kViewMargin);
//        make.top.equalTo(_bgView).offset(kViewMargin);
//        make.height.equalTo(@(kTitleLabH));
//        make.width.equalTo(@(titleLabW));
//    }];
//    
//    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_titleLabel.mas_right).offset(kViewMargin);
//        make.top.equalTo(_titleLabel);
//        make.right.equalTo(_bgView).offset(-1*kViewMargin);
//        make.bottom.equalTo(_bgView).offset(-1*kViewMargin);
//    }];
//}

#pragma mark - class method
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BIZLiveRoomTableViewCellNew *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
    }

    return cell;
}

#pragma mark - setter and getter
- (void)showMessage:(NSString *)message spokesmanType:(BIZLiveRoomTableViewCellNewSpokesmanType)type title:(NSString *)title
{
    switch (type) {
        case BIZLiveRoomTableViewCellNewSpokesmanTypeSelf:
            _titleLabel.textColor = COLOR_WORD_YELLOW_SELF;

            break;
        case BIZLiveRoomTableViewCellNewSpokesmanTypePublisher:
            _titleLabel.textColor = COLOR_WORD_BLUE_ANCHOR;

            break;
        case BIZLiveRoomTableViewCellNewSpokesmanTypeOthers:
            _titleLabel.textColor = COLOR_WORD_YELLOW_OTHER;

            break;
    
        default:
            break;
    }
    
    //  标题lable
    NSMutableAttributedString *titleAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",title] attributes:@{NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];
    _titleLabel.attributedText = titleAttStr;
    CGSize titleTextSize = [titleAttStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, kTitleLabH)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                     context:nil].size;
    CGFloat titlLabW = titleTextSize.width;
    _titleLabel.frame = CGRectMake(kViewMargin *2, kViewMargin, titlLabW, kTitleLabH);


    //  内容lable
    NSMutableAttributedString *attrStr = [Utility emotionStrWithString:message y:-8];
    [attrStr addAttribute:NSFontAttributeName
                    value:FONT(CHINESE_FONT_NAME, 14)
                    range:NSMakeRange(0, attrStr.length)];

    _contentLabel.attributedText = attrStr;

    CGFloat contentLabX = kViewMargin *3 + titlLabW;
    CGFloat contentLabW = Main_Screen_Width - (kViewMargin *5 +titlLabW);
    CGSize textSize = [attrStr boundingRectWithSize:CGSizeMake(contentLabW, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                            context:nil].size;

    CGFloat contentLabH = textSize.height;
    
    _contentLabel.frame = CGRectMake(contentLabX, kViewMargin, contentLabW, contentLabH);
    
    // 暗色背景视图
    CGFloat bgViewW = Main_Screen_Width - kViewMargin *2;
    _bgView.frame = CGRectMake(kViewMargin, 0, bgViewW, kViewMargin*2 +contentLabH);
    
}

+ (CGFloat)heightOfCellWithMessage:(NSString *)message title:(NSString *)title {
    
    CGFloat H = 0;

    //  计算标题lable宽度
    NSMutableAttributedString *attrStr1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",title] attributes:@{NSFontAttributeName:FONT(CHINESE_FONT_NAME, 14)}];
    CGSize textSize1 = [attrStr1 boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,kTitleLabH )
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                            context:nil].size;
    CGFloat titlLabW = textSize1.width;
    
    //  计算内容lable宽度
    CGFloat contentLabW = Main_Screen_Width - (kViewMargin *5 +titlLabW);
    
    NSMutableAttributedString *attrStr = [Utility emotionStrWithString:message y:-8];
    [attrStr addAttribute:NSFontAttributeName
                    value:FONT(CHINESE_FONT_NAME, 14)
                    range:NSMakeRange(0, attrStr.length)];

    CGSize textSize = [attrStr boundingRectWithSize:CGSizeMake(contentLabW, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                            context:nil].size;
    //  cell 整体高度
    H = textSize.height + kViewMargin *2 +10;
    return H;
    
}

@end
